/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.service;



import com.reddys.rad.inventory.db.Product;
import com.reddys.rad.inventory.db.ProductCategory;
import com.reddys.rad.inventory.db.PurchaseOrder;
import com.reddys.rad.inventory.db.PurchaseOrderDetails;
import com.reddys.rad.inventory.db.Vendor;
import com.reddys.rad.inventory.utils.EntityManagerImpl;
import java.util.Iterator;
import java.util.List;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author REDDYS
 */
public class PurchaseOrderService {
    
   static EntityManager em;
    public static ObservableList getProductCategoryList(){
        ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("ProductCategory.findAll");
        List productCategpryList = query.getResultList();
        Iterator iter = productCategpryList.iterator();
        while(iter.hasNext()){
            ProductCategory productCategory = (ProductCategory) iter.next();
            data.add(productCategory.getProductCategoryDescription());
        }
        return data;
    }
    
    public static PurchaseOrderDetails save(PurchaseOrderDetails purchaseOrderDetails){
        purchaseOrderDetails = (PurchaseOrderDetails) EntityManagerImpl.persist(purchaseOrderDetails);
        return purchaseOrderDetails;
    }
    
   public static PurchaseOrder saveHeader(PurchaseOrder purchaseOrder){
        purchaseOrder = (PurchaseOrder) EntityManagerImpl.persist(purchaseOrder);
                System.out.println(purchaseOrder.getPurchaseOrderId());
        return purchaseOrder;
    }

    public static ObservableList getProductList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Product.findAll");
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            Product product = (Product) iter.next();
            data.add(product.getProductId());
        }
        return data;
    }
    
        public static Product getProductListById(String productId) {
        em = EntityManagerImpl.getEntityManager();
        Product product = null;
        Query query = em.createNamedQuery("Product.findByProductId");
        query.setParameter("productId", productId);
        List productList = query.getResultList();
        Iterator iter = productList.iterator();
        while(iter.hasNext()){
            product = (Product) iter.next();
        }
        return product;
    }
        
     public static ObservableList getVendorList() {
         ObservableList data = FXCollections.observableArrayList();
        em = EntityManagerImpl.getEntityManager();
        Query query = em.createNamedQuery("Vendor.findAll");
        List vendorList = query.getResultList();
        Iterator iter = vendorList.iterator();
        while(iter.hasNext()){
            Vendor vendor = (Vendor) iter.next();
            data.add(vendor);
            System.out.println("data"+data);
            System.out.println("vendor"+vendor);
            
        }
        return data;
    }
    
}
